﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.List.PlugIn;
using Kingdee.BOS.Core.List.PlugIn.Args;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RK.ListBillPlugin.BPW
{
    [Description("BPW:自定义即时库存"), HotUpdate]
    public class InvListFromSql : AbstractListPlugIn
    {
        string filterString = "";
        public override void PrepareFilterParameter(FilterArgs e)
        {
            base.PrepareFilterParameter(e);
            filterString = e.FilterString;
            e.SQLType = 0;
        }
        public override void BeforeGetDataForTempTableAccess(BeforeGetDataForTempTableAccessArgs e)
        {
            base.BeforeGetDataForTempTableAccess(e);
            if (filterString.Length > 0)
            {
                filterString = " where " + filterString;
            }
            string createSql = GetCreateSql();
            string dataSql = $" INSERT INTO #TEMP EXEC P_ZSLH_DzInventoryWithOrderby ";
            string endSql = $" INSERT INTO {e.TableName}(FmatNumber,FmatName,FSPECIFICATION,FBOMID,FStockName,FLOT,FUnitName,F_BPW_DWGNUMBER,FBASEQTY,fformid,FID)  select FMATERIALID,FmatName,FSPECIFICATION,FBOMID,FSTOCKID,FLOT,FUNITID,F_BPW_DWGNUMBER,FBASEQTY,'POSW_BpwInv',1  from #TEMP {filterString}";
            string sql = createSql + dataSql + endSql;
            DBUtils.Execute(Context, sql);
        }

        private string GetCreateSql()
        {
            string createSql = @"/*dialect*/ CREATE TABLE #TEMP (
            FIDENTITYID INT NULL,            
            FMATERIALID VARCHAR(255) NULL,
            FmatName VARCHAR(255) NULL,
            FSPECIFICATION VARCHAR(255) NULL,
            FBOMID VARCHAR(255) NULL,
            FSTOCKID VARCHAR(255) NULL,
            FLOT VARCHAR(50) NULL,
            FUNITID VARCHAR(50) NULL,
            FBASEQTY DECIMAL(38,10) NOT NULL,
            F_BPW_DWGNUMBER VARCHAR(50) NULL
            ) ";
            return createSql;
        }
    }
}
